package models

import (
	"database/sql"
)

type RDACCOUNT struct {
	// RDACCOUNT 记录读者钱信息
	RDID      string //读者证号
	DEPOSIT   int    //读者证押金
	PREPAY    int    //预付款
	ARREARAGE int    //欠款
	SPDEPOSIT int    //
}

func RdAccountUin(db *sql.DB, data *RDACCOUNT) error {

	// 检查数据是否存在于表中
	queryExist := "SELECT COUNT(*) FROM RDACCOUNT WHERE RDID = :1"
	var count int
	err := db.QueryRow(queryExist, data.RDID).Scan(&count)
	if err != nil {
		lg.Error("RdAccountUin QueryRow", err)
		return err
	}

	if count == 0 {
		// 数据不存在，插入数据
		queryInsert := `INSERT INTO RDACCOUNT (RDID, DEPOSIT, PREPAY,ARREARAGE,SPDEPOSIT) VALUES (:1, :2, :3, :4, :5)`
		smt, err := db.Prepare(queryInsert)
		if err != nil {
			return err
		}
		defer smt.Close()
		_, err = smt.Exec(data.RDID, data.DEPOSIT, data.PREPAY, data.ARREARAGE, data.SPDEPOSIT)
		if err != nil {
			return err
		}
		lg.Debug("RdAccountUin Inserted new data", data.RDID)
	}
	return nil
}
